<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品编码" prop="goodsId">
        <el-input
          v-model="queryParams.goodsId"
          placeholder="请输入商品编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入商品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退回金额" prop="refundPrice">
        <el-input
          v-model="queryParams.refundPrice"
          placeholder="请输入退回金额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收款方式" prop="payMethod">
        <el-select v-model="queryParams.payMethod" placeholder="请选择收款方式" clearable size="small" @keyup.enter.native="handleQuery" >
          <el-option v-for="item in payMethodOptions" :value="item.v" :label="item.l" />
        </el-select>
      </el-form-item>
      <el-form-item label="收款账户" prop="payAccount">
        <el-select v-model="queryParams.payAccount" placeholder="请选择收款账户" clearable size="small" @keyup.enter.native="handleQuery">
          <el-option v-for="item in billPayeeOptions" :label="item.dictLabel" :value="item.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option v-for="item in statusOptions" :label="item.l" :value="item.v" />
        </el-select>
      </el-form-item>
      <el-form-item label="审核人" prop="checkUserName">
        <el-input
          v-model="queryParams.checkUserName"
          placeholder="请输入审核人名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sale:refund:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" border :data="refundList">
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="商品编码" align="center" prop="goodsId" width="180" />
      <el-table-column label="商品名称" align="center" prop="goodsName" show-overflow-tooltip />
      <el-table-column label="销售金额" align="center" prop="salePrice" />
      <el-table-column label="退回金额" align="center" prop="refundPrice" />
      <el-table-column label="利润" align="center" prop="profitPrice" />
      <el-table-column label="付款方式" align="center" prop="payMethod">
        <template v-slot="scope">
          <el-tag v-for="item in payMethodOptions" :type="item.t" v-if="item.v === scope.row.payMethod">{{item.l}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="付款账户" align="center" prop="payAccount" :formatter="formatterPayee" />
      <el-table-column label="状态" align="center" prop="status">
        <template v-slot="scope">
          <el-tag v-for="item in statusOptions" :type="item.t" v-if="item.v === scope.row.status">{{item.l}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="审核人名称" align="center" prop="checkUserName" />
      <el-table-column label="审核时间" align="center" prop="checkTime" width="180" />
      <el-table-column label="备注" align="center" prop="remark" show-overflow-tooltip />
      <el-table-column label="审核备注" align="center" prop="checkRemark" show-overflow-tooltip />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template v-slot="scope">
          <el-button type="text" @click="handleGotoBill(scope.row.billId)">详情</el-button>
          <el-button type="text" v-if="scope.row.status === 1" @click="handleCheck(scope.row, 2)">通过</el-button>
          <el-button type="text" v-if="scope.row.status === 1" @click="handleCheck(scope.row, 3)">作废</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改版本3 --- 退单信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品编码">{{form.goodsId}}</el-form-item>
        <el-form-item label="商品名称">{{form.goodsName}}</el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.checkRemark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRefund, exportRefund, passRefund, noPassRefund } from "@/api/sale/refund";
import {getDicts} from "@/api/system/dict/data";

export default {
  name: "Refund",
  components: {
  },
  data() {
    return {
      payMethodOptions: [
        {v:'alipay', l:'支付宝', t: ""},
        {v: 'wechat', l:'微信', t: "success"},
        {v: 'cash', l:'现金', t: "danger"},
        {v: 'bank', l:'银行卡', t: "warning"},
        {v: 'other', l:'其它', t: "info"}
      ],
      //账户类型
      billPayeeOptions: [],
      //1=待审核，2=通过，3=作废
      statusOptions: [
        {v:1, l:'待审核', t: '' },
        {v:2, l:'通过', t: 'success' },
        {v:3, l:'作废', t: 'info' }
      ],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 版本3 --- 退单信息表格数据
      refundList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsId: null,
        goodsName: null,
        salePrice: null,
        refundPrice: null,
        payMethod: null,
        payAccount: null,
        profitPrice: null,
        status: null,
        checkUserName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    getDicts("recovery_bill_payee").then(res => {
      this.billPayeeOptions = res.data;
    })
  },
  methods: {
    handleGotoBill(id) {
      this.$router.push({
        path: '/sale/bill',
        query: {detailId: id}
      })
    },
    handleCheck(row, type) {
      this.form = {
        goodsId: row.goodsId,
        goodsName: row.goodsName,
        id: row.id,
        status: type,
        checkRemark: undefined
      }
      if(type === 2) {
        this.title = row.goodsId + "审核通过";
      }else {
        this.title = row.goodsId + "审核作废";
      }
      this.open = true;
    },
    formatterPayee(row) {
      for (let i =0; i < this.billPayeeOptions.length; i++) {
        if(row.payAccount === this.billPayeeOptions[i].dictValue)
          return this.billPayeeOptions[i].dictLabel;
      }
      return "";
    },
    /** 查询版本3 --- 退单信息列表 */
    getList() {
      this.loading = true;
      listRefund(this.queryParams).then(response => {
        this.refundList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 提交按钮 */
    submitForm() {
      const data = {
        id: this.form.id,
        remark: this.form.checkRemark
      }
      if(this.form.status === 2) {
        passRefund(data).then(res => {
          this.msgSuccess("操作成功");
          this.open = false;
          this.getList();
        })
      }else {
        noPassRefund(data).then(res => {
          this.msgSuccess("操作成功");
          this.open = false;
          this.getList();
        })
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有退单信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportRefund(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
